import 'package:flutter/material.dart';
import 'package:uuid/uuid.dart';

import '../flutter_tracker.dart';

class QwsTextButton extends StatelessWidget {
  final uuid = const Uuid();
  final VoidCallback? onPressed;
  final VoidCallback? onLongPress;
  final ValueChanged<bool>? onHover;
  final ValueChanged<bool>? onFocusChange;
  final ButtonStyle? style;
  final Clip clipBehavior;
  final FocusNode? focusNode;
  final MaterialStatesController? statesController;
  final bool? isSemanticButton;
  final bool? autofocus;
  final Widget child;
  final String eventId;
  final String eventType;
  final String? extraInfo;
  final bool tracker;

  const QwsTextButton({
    super.key,
    required this.onPressed,
    required this.eventId,
    required this.eventType,
    required this.child,
    this.tracker = true,
    this.extraInfo,
    this.onLongPress,
    this.onHover,
    this.onFocusChange,
    this.style,
    this.clipBehavior = Clip.none,
    this.focusNode,
    this.autofocus,
    this.statesController,
    this.isSemanticButton = true,
  });

  void press() {
    var start = DateTime.now().millisecondsSinceEpoch;
    onPressed!();
    if (tracker) {
      FlutterTracker.instance.event(
        eventId: eventId,
        eventType: eventType,
        extraInfo: extraInfo,
        traceId: uuid.v4(),
        costTime: DateTime.now().millisecondsSinceEpoch
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return TextButton(
      onPressed: press,
      onLongPress: onLongPress,
      onFocusChange: onFocusChange,
      onHover: onHover,
      style: style,
      clipBehavior: clipBehavior,
      focusNode: focusNode,
      statesController: statesController,
      autofocus: autofocus ?? false,
      isSemanticButton: isSemanticButton,
      child: child,
    );
  }
}
